package org.jeecg.modules.dlxj.service.impl;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.dlxj.entity.DlxBugQuery;
import org.jeecg.modules.dlxj.entity.DlxjDebugQuery;
import org.jeecg.modules.dlxj.mapper.DlxBugQueryMapper;
import org.jeecg.modules.dlxj.service.IDlxBugQueryService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 缺陷查询
 * @Author: lzl
 * @Date:   2022-07-27
 * @Version: V1.0
 */
@Service
public class DlxBugQueryServiceImpl extends ServiceImpl<DlxBugQueryMapper, DlxBugQuery> implements IDlxBugQueryService {

    @Autowired
    DlxBugQueryMapper dlxBugQueryMapper;

    @Value("${jeecg.path.upload}")
    private String upLoadPath;


    /**
     * 缺陷查询列表
     * @return
     */
    @Override
    public List<DlxBugQuery> bugQueryList() {
        return dlxBugQueryMapper.bugQueryList();

    }

    /**
     * 缺陷查询，多条件查询
     * @param taskId
     * @param circuitryId
     * @param level
     * @param bugType
     * @return
     */
    @Override
    public List<DlxBugQuery> queryByManyConditions(String taskId, String circuitryId, Integer level, String bugType) {
        return dlxBugQueryMapper.queryByManyConditions(taskId,circuitryId,level,bugType);
    }

    @Override
    public ModelAndView exportExcel(HttpServletRequest request, DlxBugQuery dlxBugQuery, Class<DlxBugQuery> clazz, String title, String taskId, String circuitryId, Integer level, String bugType) {
        // Step.1 组装查询条件
        //QueryWrapper<DlxjDebugStatistics> queryWrapper = QueryGenerator.initQueryWrapper(dlxjDebugStatistics, request.getParameterMap());
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        // Step.2 获取导出数据
        List<DlxBugQuery> pageList;
        if (taskId!=null || circuitryId!=null || level!=null || bugType!=null){
            pageList = dlxBugQueryMapper.queryByManyConditions(taskId,circuitryId,level,bugType);}
        else{
            pageList = dlxBugQueryMapper.bugQueryList();
        }

        List<DlxBugQuery> exportList = null;

        //过滤选中数据
        String selections = request.getParameter("selections");
        if (oConvertUtils.isNotEmpty(selections)) {
            List<String> selectionList = Arrays.asList(selections.split(","));
            exportList = pageList.stream().filter(item -> selectionList.contains(item.getPoleId())).collect(Collectors.toList());
        } else {
            exportList = pageList;
        }

        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.FILE_NAME, title);
        mv.addObject(NormalExcelConstants.CLASS, clazz);
        //update-begin--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置--------------------
        ExportParams exportParams=new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title);
        exportParams.setImageBasePath(upLoadPath);
        //update-end--Author:liusq  Date:20210126 for：图片导出报错，ImageBasePath未设置----------------------
        mv.addObject(NormalExcelConstants.PARAMS,exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }
}
